package com.msb.mall.member.controller;

import com.alibaba.fastjson.JSON;
import com.msb.common.exception.BizCodeEnum;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;
import com.msb.mall.member.entity.MemberEntity;
import com.msb.mall.member.entity.vo.MemberLoginVo;
import com.msb.mall.member.entity.vo.MemberRegisterVo;
import com.msb.mall.member.entity.vo.SocialUser;
import com.msb.mall.member.exception.PhoneExistException;
import com.msb.mall.member.exception.UserNameExistException;
import com.msb.mall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 会员
 *
 * @author lwq
 * @email 1907326957@qq.com
 * @date 2024-06-12 22:25:31
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;


    /**
     *会员注册
     */
    @RequestMapping("/register")
    public R register(@RequestBody MemberRegisterVo vo){
        try {
            memberService.register(vo);
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }catch (UserNameExistException e) {
            return R.error(BizCodeEnum.USER_NAME_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_NAME_EXIST_EXCEPTION.getMsg());
        }catch (Exception e) {
            return R.error(BizCodeEnum.UNKNOWN_EXCEPTION.getCode(),BizCodeEnum.UNKNOWN_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @RequestMapping("/login")
    public  R login(@RequestBody MemberLoginVo vo){
        MemberEntity entity = memberService.login(vo);
        if(entity != null ){
            return R.ok().put("entity", JSON.toJSONString(entity));
        }
        return R.error(BizCodeEnum.PHONE_NOT_NAME_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_NAME_EXIST_EXCEPTION.getMsg());

    }

    @RequestMapping("/oauth2/login")
    public  R oauth2Login(@RequestBody SocialUser user){
        MemberEntity entity = memberService.oauth2Login(user);
        return R.ok().put("entity", JSON.toJSONString(entity));
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
